using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Deepcode.Flux.Core.Services.FluxClient;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// HostTable is a special type of container. Where you want to use sections and
	/// panels that may go off the edge of the page width, only the section that has the
	/// extended width content will actually expand to the width of the page without a container.
	/// That container can be either a PanelBox or one of these HostTable's. Difference being a
	/// HostTable renders nothing except a 100% width table to force it's contents to fill to the
	/// max width of it's content.
	/// </summary>
	[ParseChildren(false)]
	public class HostTable : Deepcode.Flux.Core.UI.Controls.Base.BaseControl
	{
		#region Start / End Rendering
		/// <summary>
		/// Render the opening of the panel
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
		{
			RenderOpenTable(writer);
		}
		/// <summary>
		/// Render the closing of the panel
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			RenderCloseTable(writer);
		}
		#endregion
		#region Virtual methods for rendering
		/// <summary>
		/// Renders the beginning of the panel's encapsulating table
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderOpenTable( HtmlTextWriter writer )
		{
			string sWidth = "";
			string sHeight = "";
			if( this.Width != Unit.Empty ) sWidth = " width='" + this.Width.ToString() + "'";
			else sWidth = " width='100%'";

			if( this.Height != Unit.Empty ) sHeight = " height='" + this.Height.ToString() + "'";
			
			writer.Write( "<table cellspacing=0 cellpadding=0 border=0{0}{1}>", sWidth, sHeight );
			writer.Write( "<tr><td width='100%' height='100%' valign='top' class='HostPaneContent'>");
		}
		/// <summary>
		/// Renders the closing of the panel's encapsulating table
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void RenderCloseTable( HtmlTextWriter writer )
		{
			writer.Write("</td></tr></table>");
		}
		#endregion
		#region Initialisation
		/// <summary>
		/// Init will ensure that the common UI stylesheets etc are included
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			this.LoadClientStyleSheet("HostTable/HostTable.css", "Deepcode.Flux.Core.UI.Controls.HostTable");		
		}
		#endregion
	}
}
